package org.zjt.review

/**
  * DESC    
  *
  * @author
  * @create 2017-06-20 下午5:38
  **/
object ArrayTest extends App {

  var array = new Array[String](5)
  for (i <- 1 until 5 if i % 2 == 0) {
    array(i) = "zhang" + i
  }

  array.foreach(a => a match {
    case s: String => println(s)
    case null => println("空")
  })


  // Range(开始数值，结束数值，step)   返回一个数组。
  val array2 = Array(1.9, 2.9, 3.4, 3.5)
  val array3 = Range(10, 20)
  val array4 = Range(10, 20, 2)

  array2.foreach(println)
  array3.foreach(println)
  array4.foreach(println)


  for (i <- 0 to (array.length - 1)) {
    println(array(i))
  }


  //多维数组
  var array5: Array[Array[Int]] = Array.ofDim[Int](5, 3)
  for (i <- 0 until 5) {
    for (j <- 0 until 3) {
      array5(i)(j) = i + j
    }
  }

  var array6: Array[Array[Int]] = new Array[Array[Int]](5)
  for (i <- 0 until 5) {
    array6(i) = Array(2,3,43,2,1)
  }

  array5.foreach(println(_))
  array6.foreach(println(_))




  val scores = Map("Alice" -> 99, "Spark" -> 100)
  /*//get("Alice")获取的是一个Option。如果有Alice这个key，就返回Some(99),否则返回None
  println(scores.get("Alice")) //Some(99)
  println(scores.get("Alice").get) //99
  println(scores.get("Hadoop")) //None
  //println(scores.get("Hadoop").get) // Exception in thread "main" java.util.NoSuchElementException: None.get*/
  scores.get("Alice") match {
    //用Option的子类来进行模式匹配
    case Some(score) => println(score)
    case None => println("No score")
  }

}
